/**
 * 
 */
package GUI;

import javax.swing.JPanel;
import java.awt.*;
import javax.swing.*;

import Entity.Customer;
import Entity.User;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;


/**
 * @author aviat
 *
 */
public class SetShiftGui extends JPanel{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 
	 */
	
	private JButton btnBack;
	private JTextField lNametextField;

	private JLabel lblFirstMagnets;
	private JLabel lblSecondMagnets;
	private JLabel lblFirstVideo;
	private JComboBox<String> video2comboBox;
	

	private JLabel iconlabel;
	private JLabel lblStills;
	private JLabel lblNumofVideo;
	private JComboBox<String> magnets1comboBox;
	private JComboBox<String> magnets2ComboBox;
	private JComboBox<String> stillsComboBox;
	private JComboBox<String> video1ComboBox;
	private JButton btnRefresh;
	JTextPane commentTextPane;
	
	private JCheckBox chckbxMagnets;
	private JCheckBox chckbxHoverCamera;
	private JRadioButton radioButton1;
	private JRadioButton radioButton2;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	
	private ArrayList<User> w_allList;
	private ArrayList<User> w_filteredDate;
	private Object syncObject;
	private boolean w_isChangeAllowed;
	private JLabel labelmagnets2;
	private JButton btnSet;
	private JComboBox<String> TeamLeadercomboBox;
	private JScrollPane scrollPane;
	private JLabel lblComment;

	
	public SetShiftGui() {
		
	
		setBackground(new Color(255, 51, 0));
		setLayout(null);
		btnBack = new JButton("Back");
		btnBack.setIcon(new ImageIcon(ViewCustomerDetails.class.getResource("/resources/back22.png")));
		lNametextField = new JTextField();
		btnRefresh = new JButton("Refresh");
		magnets1comboBox = new JComboBox<String>();
		magnets2ComboBox = new JComboBox<String>();
		lNametextField.setEditable(false);
		stillsComboBox = new JComboBox<String>();
		video2comboBox = new JComboBox<String>();
		video1ComboBox = new JComboBox<String>();
		TeamLeadercomboBox = new JComboBox<String>();
		
		TeamLeadercomboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
		initialize();
	}

	private void initialize() {


		JLabel lblSetShift = new JLabel("Set Shift Arrangement");
		lblSetShift.setForeground(new Color(0, 204, 255));
		lblSetShift.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblSetShift.setBounds(10, 11, 211, 44);
		add(lblSetShift);
		
		btnBack.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnBack.setBounds(10, 462, 101, 27);
		add(btnBack);
		

		lblFirstMagnets = new JLabel("Magnets Phothographer 1");
		lblFirstMagnets.setForeground(Color.WHITE);
		lblFirstMagnets.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblFirstMagnets.setBounds(341, 119, 211, 22);
		add(lblFirstMagnets);
		
		lblSecondMagnets = new JLabel("Magnets Phothographer 2");
		lblSecondMagnets.setForeground(Color.WHITE);
		lblSecondMagnets.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblSecondMagnets.setBounds(341, 170, 211, 22);
		add(lblSecondMagnets);
		
		lblFirstVideo = new JLabel("Video Phothographer 1");
		lblFirstVideo.setForeground(Color.WHITE);
		lblFirstVideo.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblFirstVideo.setBounds(10, 139, 156, 22);
		add(lblFirstVideo);

		ImageIcon LOGOImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resources/camera-background3.png"));
		ImageIcon LOGO = new ImageIcon (LOGOImageIcon.getImage().getScaledInstance(-1, -1, java.awt.Image.SCALE_SMOOTH));
		
		lblStills = new JLabel("Stills Phothographer");
		lblStills.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblStills.setForeground(Color.WHITE);
		lblStills.setBounds(10, 276, 127, 26);
		add(lblStills);
		
		lblNumofVideo = new JLabel("Number Of Video Photographers");
		lblNumofVideo.setForeground(Color.WHITE);
		lblNumofVideo.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblNumofVideo.setBounds(10, 66, 216, 22);
		add(lblNumofVideo);
		
		
		magnets1comboBox.setEnabled(false);
		magnets1comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed()){
						System.out.println("in action magnet1");
						filterList(magnets2ComboBox,magnets1comboBox.getSelectedItem().toString(),"MAGNETS2");
						
						magnets2ComboBox.setEnabled(true);
					}
				}
			}
		});
		magnets1comboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
		magnets1comboBox.setBounds(527, 117, 101, 27);
		add(magnets1comboBox);
		
		
		magnets2ComboBox.setEnabled(false);

		magnets2ComboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
		magnets2ComboBox.setBounds(527, 168, 101, 27);
		add(magnets2ComboBox);
		
		
//		stillsComboBox.addActionListener(new ActionListener() {
//			public void actionPerformed(ActionEvent e) {
//				
//				synchronized (syncObject) {
//					if(isChangeAllowed()){
//						System.out.println("in action stills");
//						filterList(stillsComboBox,null,"STILLS");
//					}
//				}
//			}
//		});
		stillsComboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
		stillsComboBox.setBounds(176, 276, 101, 27);
		add(stillsComboBox);
		
		
		video2comboBox.setEnabled(false);

		video2comboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
		video2comboBox.setBounds(176, 184, 101, 27);
		add(video2comboBox);
		
		
		video1ComboBox.setEnabled(false);
		video1ComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.out.println("in action video1");
				synchronized (syncObject) {
					if(isChangeAllowed()){
						System.out.println("in action video2");
						filterList(video2comboBox,video1ComboBox.getSelectedItem().toString(),"VIDEO2");		
						if(radioButton2.isSelected())
							video2comboBox.setEnabled(true);
					}
				}
			}
		});
		video1ComboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
		video1ComboBox.setBounds(176, 137, 101, 27);
		add(video1ComboBox);
		
		
		btnRefresh.setIcon(new ImageIcon(ViewCustomerDetails.class.getResource("/resources/arrow-refresh-2-icon.png")));
		btnRefresh.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnRefresh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed()){
						setCombobox();
					}
				}
			}
		});
		btnRefresh.setBounds(193, 419, 120, 27);
		add(btnRefresh);
		
	    chckbxMagnets = new JCheckBox("Magnets");
	    chckbxMagnets.addActionListener(new ActionListener() {
	    	public void actionPerformed(ActionEvent e) {
	    		if(chckbxMagnets.isSelected())
	    		{
	   				magnets1comboBox.setEnabled(true);
	    		}
	    		else
	    		{
	    			magnets1comboBox.setEnabled(false);
	    			magnets2ComboBox.setEnabled(false);
	    		}
	    	}
	    });
		chckbxMagnets.setBounds(527, 67, 85, 23);
		add(chckbxMagnets);
		
		chckbxHoverCamera = new JCheckBox("Hover camera");
		chckbxHoverCamera.setBounds(527, 267, 101, 23);
		add(chckbxHoverCamera);
		
		labelmagnets2 = new JLabel("Video Phothographer 2");
		labelmagnets2.setForeground(Color.WHITE);
		labelmagnets2.setFont(new Font("Tahoma", Font.BOLD, 12));
		labelmagnets2.setBounds(10, 185, 156, 22);
		add(labelmagnets2);
		
		radioButton1 = new JRadioButton("1");
		buttonGroup.add(radioButton1);
		radioButton1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(radioButton1.isSelected())
				{
					video1ComboBox.setEnabled(true);
					video2comboBox.setEnabled(false);
				}
			}
		});
		radioButton1.setBounds(239, 66, 36, 23);
		add(radioButton1);
		
		radioButton2 = new JRadioButton("2");
		buttonGroup.add(radioButton2);
		radioButton2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(radioButton2.isSelected())
				{
					video1ComboBox.setEnabled(true);
				}
			}
		});
		radioButton2.setBounds(239, 92, 36, 23);
		add(radioButton2);
		
		btnSet = new JButton("Apply");
		btnSet.setIcon(new ImageIcon(SetShiftGui.class.getResource("/resources/dialog_apply.png")));
		btnSet.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnSet.setBounds(345, 419, 120, 27);
		btnSet.setEnabled(true);
		add(btnSet);
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(527, 329, 196, 104);
		add(scrollPane);
		
		commentTextPane = new JTextPane();
		scrollPane.setViewportView(commentTextPane);
		
		JLabel lblTeamLeader = new JLabel("Team Leader");
		lblTeamLeader.setForeground(Color.WHITE);
		lblTeamLeader.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblTeamLeader.setBounds(10, 346, 87, 27);
		add(lblTeamLeader);
		
		TeamLeadercomboBox.setBounds(176, 346, 101, 27);
		add(TeamLeadercomboBox);
		
		lblComment = new JLabel("Comments");
		lblComment.setForeground(Color.WHITE);
		lblComment.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblComment.setBounds(418, 327, 79, 27);
		add(lblComment);
		
		iconlabel = new JLabel("");
		iconlabel.setIcon(LOGO);
		iconlabel.setBounds(0, 0, 786, 500);
		add(iconlabel);
	}

	/**
	 * @return the video2comboBox
	 */
	public JComboBox<String> getVideo2comboBox() {
		return video2comboBox;
	}

	/**
	 * @return the magnets1comboBox
	 */
	public JComboBox<String> getMagnets1comboBox() {
		return magnets1comboBox;
	}

	/**
	 * @return the magnets2ComboBox
	 */
	public JComboBox<String> getMagnets2ComboBox() {
		return magnets2ComboBox;
	}

	/**
	 * @return the stillsComboBox
	 */
	public JComboBox<String> getStillsComboBox() {
		return stillsComboBox;
	}

	/**
	 * @return the video1ComboBox
	 */
	public JComboBox<String> getVideo1ComboBox() {
		return video1ComboBox;
	}

	/**
	 * @return the chckbxMagnets
	 */
	public JCheckBox getChckbxMagnets() {
		return chckbxMagnets;
	}

	/**
	 * @return the chckbxHoverCamera
	 */
	public JCheckBox getChckbxHoverCamera() {
		return chckbxHoverCamera;
	}

	/**
	 * @return the radioButton1
	 */
	public JRadioButton getRadioButton1() {
		return radioButton1;
	}

	/**
	 * @return the radioButton2
	 */
	public JRadioButton getRadioButton2() {
		return radioButton2;
	}

	public void setWorkerList(ArrayList<User> list){
		w_allList = list;
		w_filteredDate = new ArrayList<>();
		syncObject = new Object();
		setCombobox();
	}
	/**
	* This method initials the magnets1's, vidoe1's,teemleader's and stills's combobox
	* 
	*/
	public void setCombobox(){
		magnets1comboBox.removeAllItems();
		magnets2ComboBox.removeAllItems();
		video1ComboBox.removeAllItems();
		stillsComboBox.removeAllItems();
		TeamLeadercomboBox.removeAllItems();
		w_isChangeAllowed = false;
		synchronized (syncObject) {
			for (User u :w_allList) 
			{
				if(u.getRole().equals("Magnets"))
				{
					magnets1comboBox.addItem(u.getName()+" "+u.getLName());
					//magnets2ComboBox.addItem(u.getName()+" "+u.getLName());
				}
				if(u.getRole().equals("Video"))
				{
					video1ComboBox.addItem(u.getName()+" "+u.getLName());
					//video2comboBox.addItem(u.getName()+" "+u.getLName());
				}
				if(u.getRole().equals("Stills"))
				{
					stillsComboBox.addItem(u.getName()+" "+u.getLName());
				}
				if(u.getRole().equals("Manager"))
				{
					TeamLeadercomboBox.addItem(u.getName()+" "+u.getLName());
				}
			}
			
		}
		w_isChangeAllowed = true;
	}
	/**
	* This method initials combobox with the correct values
	* @param combo is the combobox to initials
	* @param selecteditem is the selected item from combo
	* @param scope is a key to know what to choose is switch order
	* **/
	private void filterList(JComboBox<String> combo,String selectedItem, String scope)
	{
		w_isChangeAllowed = false;
		System.out.println("in filter");
		synchronized (syncObject) {
			combo.removeAllItems();
			
			switch(scope)
			{
			case "MAGNETS2":
				System.out.println("selectedItem m2: "+selectedItem);
				for (User u :w_allList) 
				{
					System.out.println(" in forr selectedItem m2: "+selectedItem);
					if(u.getRole().equals("Magnets"))
					{
						System.out.println(" in if selectedItem m2: "+selectedItem);
						
						if(!(selectedItem.equals(u.getName()+" "+u.getLName()))){
							combo.addItem(u.getName()+" "+u.getLName());
							System.out.println(u.getName()+" "+u.getLName());
						}
					}
				}
				break;
		case "VIDEO2":System.out.println("selectedItem v2 "+selectedItem);
			
				for (User u :w_allList) 
				{
					if(u.getRole().equals("Video"))
					{
						if(!(selectedItem.equals(u.getName()+" "+u.getLName())))
							combo.addItem(u.getName()+" "+u.getLName());
					}
				}
				break;
			}
		}

		w_isChangeAllowed = true;

		System.out.println("out of filter");
	}

	/**
	* @return the e_isChangeAllowed
	* 
	*/
	private boolean isChangeAllowed()
	{
		return w_isChangeAllowed;
	}

	/**
	 * @return the customerIDcomboBox
	 */
	public JComboBox<String> getCustomerIDcomboBox() {
		return magnets1comboBox;
	}

	/**
	 * @param customerIDcomboBox the customerIDcomboBox to set
	 */
	public void setCustomerIDcomboBox(JComboBox<String> customerIDcomboBox) {
		magnets1comboBox = customerIDcomboBox;
	}


	/**
	 * @return the bLnamecomboBox
	 */
	public JComboBox<String> getbLnamecomboBox() {
		return video2comboBox;
	}

	/**
	 * @return the gFnameComboBox
	 */
	public JComboBox<String> getgFnameComboBox() {
		return magnets2ComboBox;
	}

	/**
	 * @return the gLnameComboBox
	 */
	public JComboBox<String> getgLnameComboBox() {
		return stillsComboBox;
	}

	/**
	 * @return the bFnameComboBox
	 */
	public JComboBox<String> getbFnameComboBox() {
		return video1ComboBox;
	}

	/**
	 * @return the btnBack
	 */
	public JButton getBtnBack() {
		return btnBack;
	}

	/**
	 * @return the btnSet
	 */
	public JButton getBtnSet() {
		return btnSet;
	}

	/**
	 * @return the commentTextPane
	 */
	public JTextPane getCommentTextPane() {
		return commentTextPane;
	}

	/**
	 * @return the btnRefresh
	 */
	public JButton getBtnRefresh() {
		return btnRefresh;
	}

	/**
	 * @param btnRefresh the btnRefresh to set
	 */
	public void setBtnRefresh(JButton btnRefresh) {
		this.btnRefresh = btnRefresh;
	}

	/**
	 * @return the w_allList
	 */
	public ArrayList<User> getW_allList() {
		return w_allList;
	}

	/**
	 * @param w_allList the w_allList to set
	 */
	public void setW_allList(ArrayList<User> w_allList) {
		this.w_allList = w_allList;
	}

	/**
	 * @return the w_filteredDate
	 */
	public ArrayList<User> getW_filteredDate() {
		return w_filteredDate;
	}

	/**
	 * @param w_filteredDate the w_filteredDate to set
	 */
	public void setW_filteredDate(ArrayList<User> w_filteredDate) {
		this.w_filteredDate = w_filteredDate;
	}

	/**
	 * @return the w_isChangeAllowed
	 */
	public boolean isW_isChangeAllowed() {
		return w_isChangeAllowed;
	}

	/**
	 * @param w_isChangeAllowed the w_isChangeAllowed to set
	 */
	public void setW_isChangeAllowed(boolean w_isChangeAllowed) {
		this.w_isChangeAllowed = w_isChangeAllowed;
	}

	/**
	 * @return the teamLeadercomboBox
	 */
	public JComboBox<String> getTeamLeadercomboBox() {
		return TeamLeadercomboBox;
	}

	/**
	 * @param teamLeadercomboBox the teamLeadercomboBox to set
	 */
	public void setTeamLeadercomboBox(JComboBox<String> teamLeadercomboBox) {
		TeamLeadercomboBox = teamLeadercomboBox;
	}

	/**
	 * @return the buttonGroup
	 */
	public ButtonGroup getButtonGroup() {
		return buttonGroup;
	}

	/**
	 * @param btnBack the btnBack to set
	 */
	public void setBtnBack(JButton btnBack) {
		this.btnBack = btnBack;
	}

	/**
	 * @param video2comboBox the video2comboBox to set
	 */
	public void setVideo2comboBox(JComboBox<String> video2comboBox) {
		this.video2comboBox = video2comboBox;
	}

	/**
	 * @param magnets1comboBox the magnets1comboBox to set
	 */
	public void setMagnets1comboBox(JComboBox<String> magnets1comboBox) {
		this.magnets1comboBox = magnets1comboBox;
	}

	/**
	 * @param magnets2ComboBox the magnets2ComboBox to set
	 */
	public void setMagnets2ComboBox(JComboBox<String> magnets2ComboBox) {
		this.magnets2ComboBox = magnets2ComboBox;
	}

	/**
	 * @param stillsComboBox the stillsComboBox to set
	 */
	public void setStillsComboBox(JComboBox<String> stillsComboBox) {
		this.stillsComboBox = stillsComboBox;
	}

	/**
	 * @param video1ComboBox the video1ComboBox to set
	 */
	public void setVideo1ComboBox(JComboBox<String> video1ComboBox) {
		this.video1ComboBox = video1ComboBox;
	}

	/**
	 * @param commentTextPane the commentTextPane to set
	 */
	public void setCommentTextPane(JTextPane commentTextPane) {
		this.commentTextPane = commentTextPane;
	}

	/**
	 * @param chckbxMagnets the chckbxMagnets to set
	 */
	public void setChckbxMagnets(JCheckBox chckbxMagnets) {
		this.chckbxMagnets = chckbxMagnets;
	}

	/**
	 * @param chckbxHoverCamera the chckbxHoverCamera to set
	 */
	public void setChckbxHoverCamera(JCheckBox chckbxHoverCamera) {
		this.chckbxHoverCamera = chckbxHoverCamera;
	}

	/**
	 * @param radioButton1 the radioButton1 to set
	 */
	public void setRadioButton1(JRadioButton radioButton1) {
		this.radioButton1 = radioButton1;
	}

	/**
	 * @param radioButton2 the radioButton2 to set
	 */
	public void setRadioButton2(JRadioButton radioButton2) {
		this.radioButton2 = radioButton2;
	}

	/**
	 * @param btnSet the btnSet to set
	 */
	public void setBtnSet(JButton btnSet) {
		this.btnSet = btnSet;
	}
}
